package com.jz.community.moduleshoppingguide.home.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpFragment;
import com.jz.community.basecomm.bean.RegionsBean;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.commview.view.MyStateView;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.R2;
import com.jz.community.moduleshoppingguide.home.bean.PreferentialBean;
import com.jz.community.moduleshoppingguide.home.presenter.PreferentialPresenter;
import com.jz.community.moduleshoppingguide.home.ui.PreferentialView;
import com.jz.community.moduleshoppingguide.home.ui.adapter.PreferentialPageAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;

public class PreferentialFragment extends BaseMvpFragment<PreferentialView.View, PreferentialPresenter> implements PreferentialView.View, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R2.id.rv_preferential)
    RecyclerView rvPreferential;
    @BindView(R2.id.preferential_refresh)
    SmartRefreshLayout preferentialRefresh;
    PreferentialPageAdapter preferentialPageAdapter;
    private int page = 0;
    private int size = 10;
    MyStateView myStateView;
    private View noDataView;
    private View errorView;
    private View loadingView;
    String message;
    RegionsBean.EmbeddedBean.ContentBean region;


    public static PreferentialFragment newInstance(String message) {
        Bundle bundle = new Bundle();
        bundle.putString("message", message);
        PreferentialFragment fragment = new PreferentialFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.module_shoppingguide_fragment_preferential;
    }

    @Override
    protected PreferentialPresenter createPresenter() {
        return new PreferentialPresenter(this);
    }


    @Override
    protected void initView() {
        region = BaseSpUtils.getInstance().getRegion(getActivity());
        preferentialRefresh.setOnRefreshListener(this);//刷新监听
        preferentialRefresh.setEnableLoadMore(false);
        rvPreferential.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        preferentialPageAdapter = new PreferentialPageAdapter(R.layout.module_shoppingguide_item_shopping_guide, new ArrayList<>());
        rvPreferential.setAdapter(preferentialPageAdapter);
        preferentialPageAdapter.setOnLoadMoreListener(this, rvPreferential);
        myStateView = new MyStateView();
        noDataView = myStateView.getNoDataView(getActivity(), (ViewGroup) rvPreferential.getParent(), "暂无商品！", null);
        loadingView = myStateView.getLoadingView(getActivity(), (ViewGroup) rvPreferential.getParent(), R.drawable.loading_default);
        errorView = myStateView.getErrorView(getActivity(), (ViewGroup) rvPreferential.getParent(), v -> {
            page = 0;
            preferentialPageAdapter.setEmptyView(loadingView);
            mPresenter.getPreferential(page, size, region.getId() + "", message, AppConstants.ONREFRESH);
        });
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        message = getArguments().getString("message");
        if (region != null) {
            page = 0;
            mPresenter.getPreferential(page, size, region.getId() + "", message, AppConstants.ONREFRESH);
        } else {
            preferentialPageAdapter.setEmptyView(noDataView);
        }
    }

    @Override
    public void showPreferential(PreferentialBean preferentialBean, int type) {
        preferentialRefresh.finishRefresh();
        if (preferentialBean != null && preferentialBean.get_embedded() != null && preferentialBean.get_embedded().getContent() != null && preferentialBean.get_embedded().getContent().size() > 0) {
            if (type == AppConstants.ONREFRESH) {
                preferentialPageAdapter.setNewData(preferentialBean.get_embedded().getContent());
            } else {
                preferentialPageAdapter.addData(preferentialBean.get_embedded().getContent());
                preferentialPageAdapter.loadMoreComplete();
            }
        } else {
            if (type == AppConstants.ONLOADMORE) {
                preferentialPageAdapter.loadMoreEnd();
            }
        }
        if (preferentialPageAdapter.getItemCount() == 0) {
            preferentialPageAdapter.setEmptyView(noDataView);
        }
    }

    @Override
    public void onFail(String s) {
        page = 0;
        preferentialPageAdapter.setEmptyView(errorView);
        preferentialPageAdapter.loadMoreEnd();
        preferentialRefresh.finishRefresh();
    }


    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        page = 0;
        mPresenter.getPreferential(page, size, region.getId() + "", message, AppConstants.ONREFRESH);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        mPresenter.getPreferential(page, size, region.getId() + "", message, AppConstants.ONLOADMORE);
    }
}
